package com.woniuxy.joylifemarket.controller;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifemarket.service.CenterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    CenterUserService centerUserService;

    //用户信息
    @RequestMapping("/inituser")
    public Object userInfo(String userid) {
        Map<String, Object> result = new HashMap<>();
        try {
            CenterUser user = centerUserService.findUserById(userid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", user);
//            System.out.println(user);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //加载当前登录用户信息
    @RequestMapping("/initloginuser")
    public Object loginUser(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            //去token中获取登录用户
            String userid= JWTUtils.getUserId(request.getHeader("token"));
//            System.out.println("userid:"+userid);
            CenterUser user = centerUserService.findUserById(userid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data",user);
//            System.out.println("user"+user);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

}
